module.exports = function(window, document) {
	var apivm = require('../../common/avm.min')(window, document);
	var avm = apivm;
	var api = apivm.api;
	var Component = apivm.Component;
	var $ = apivm.$;
	(function() {
	/**
	 * 判断当前是否是小程序环境
	 * 2020-11-24  暂用 api.uiMode模拟
	 * 后期需修正为  api.platform
	 * @returns {boolean}
	 */
	function isMP() {
		return !api.uiMode;
	}

	/**
	 * 统一设置底部tabBar的数字小红点
	 * @param index
	 * @param text
	 */
	function setTabBarBadge(index, text) {
		text += "";
		if (isMP()) {
			if (text === "0") {
				wx.removeTabBarBadge({index: index});
			} else {
				wx.setTabBarBadge({index: index, text: text});
			}
		} else {
			api.setTabBarItemAttr({index: index, badge: {text: text, color: "#FFF"}});
		}
	}

	function ownKeys(object, enumerableOnly) {
		var keys = Object.keys(object);

		if (Object.getOwnPropertySymbols) {
			var symbols = Object.getOwnPropertySymbols(object);

			if (enumerableOnly) {
				symbols = symbols.filter(function(sym) {
					return Object.getOwnPropertyDescriptor(object, sym).enumerable;
				});
			}

			keys.push.apply(keys, symbols);
		}

		return keys;
	}

	function _objectSpread2(target) {
		for (var i = 1; i < arguments.length; i++) {
			var source = arguments[i] != null ? arguments[i] : {};

			if (i % 2) {
				ownKeys(Object(source), true).forEach(function(key) {
					_defineProperty(target, key, source[key]);
				});
			} else if (Object.getOwnPropertyDescriptors) {
				Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
			} else {
				ownKeys(Object(source)).forEach(function(key) {
					Object.defineProperty(
						target,
						key,
						Object.getOwnPropertyDescriptor(source, key)
					);
				});
			}
		}

		return target;
	}

	function _defineProperty(obj, key, value) {
		if (key in obj) {
			Object.defineProperty(obj, key, {
				value: value,
				enumerable: true,
				configurable: true,
				writable: true
			});
		} else {
			obj[key] = value;
		}

		return obj;
	}

	var config = {
		schema: "https",
		host: "a7777777777777-pd.apicloud-saas.com",
		path: "api"
	};

	function req(options) {
		var baseUrl = config.schema + "://" + config.host + "/" + config.path + "/";
		options.url = baseUrl + options.url;
		return new Promise(function(resolve, reject) {
			var Cookie = api.getPrefs({sync: true, key: "COOKIE"});
			if (Cookie) {
				if (options.headers) {
					options.headers.Cookie = Cookie;
				} else {
					options.headers = {Cookie: Cookie};
				}
			}
			api.ajax(options, function(ret, err) {
				console.log(
					"[" +
						options.method +
						"] " +
						options.url +
						" [" +
						api.winName +
						"/" +
						api.frameName +
						"]\n" +
						JSON.stringify(
							_objectSpread2(_objectSpread2({}, options), {}, {ret: ret, err: err})
						)
				);

				if (ret) {
					if (ret.status === 0 || ret.code === 0) {
						resolve(ret.data ? ret.data : ret);
					} else {
						reject(ret);
						if (typeof options.error === "function") {
							options.error(ret);
						} else {
							api.alert({
								title: ret.msg,
								msg: "\u670D\u52A1\u7AEF\u9519\u8BEF\u4EE3\u7801:[" + ret.code + "]"
							});
						}
					}
				} else {
					var errNames = [
						"连接错误",
						"超时",
						"授权错误",
						"数据类型错误",
						"不安全的数据"
					];
					console.log([errNames[err.code], JSON.stringify(err)]);
					reject(err);

					if (typeof options.fail === "function") {
						options.fail(err);
					} else {
						api.toast({
							msg: errNames[err.code] + "[" + err.code + "]\n" + err.msg,
							location: "top",
							duration: 2500
						});
					}
				}
			});
		});
	}

	/**
	 * GET请求快捷方法
	 * @constructor
	 * @param url {string} 地址
	 * @param options {Object} 附加参数
	 */
	function GET(url, options) {
		if (options === void 0) {
			options = {};
		}
		return req(
			_objectSpread2(_objectSpread2({}, options), {}, {url: url, method: "GET"})
		);
	}

	var GoodsAction = /*@__PURE__*/ (function(Component) {
		function GoodsAction(props) {
			Component.call(this, props);
		}

		if (Component) GoodsAction.__proto__ = Component;
		GoodsAction.prototype = Object.create(Component && Component.prototype);
		GoodsAction.prototype.constructor = GoodsAction;
		GoodsAction.prototype.crash = function() {
			var pageParam = {list: [this.props]};
			api.openWin({
				name: "pending_order",
				url: "../../pages/pending_order/pending_order.stml",
				pageParam: pageParam
			});
		};
		GoodsAction.prototype.addCart = function() {
			this.fire("addCart");
		};
		GoodsAction.prototype.render = function() {
			return apivm.h(
				"safe-area",
				{class: "action-bar"},

				apivm.h(
					"view",
					{class: "action-btn btn-add-cart", onClick: this.addCart},
					apivm.h("text", {class: "btn-text"}, "加入购物车")
				),
				apivm.h(
					"view",
					{class: "action-btn btn-crash", onClick: this.crash},
					apivm.h("text", {class: "btn-text"}, "结算")
				)
			);
		};

		return GoodsAction;
	})(Component);
	apivm.define("goods-action", GoodsAction);

	var GoodsCounter = /*@__PURE__*/ (function(Component) {
		function GoodsCounter(props) {
			Component.call(this, props);
		}

		if (Component) GoodsCounter.__proto__ = Component;
		GoodsCounter.prototype = Object.create(Component && Component.prototype);
		GoodsCounter.prototype.constructor = GoodsCounter;
		GoodsCounter.prototype.countChange = function(change) {
			if (this.props.count + change === 0) {
				return api.toast({
					msg: "不能再减少了\n可在购物车编辑模式下移除",
					location: "middle"
				});
			}
			this.fire("CountChange", {
				change: change,
				props: this.props
			});
		};
		GoodsCounter.prototype.render = function() {
			var this$1 = this;
			return apivm.h(
				"view",
				{class: "flex-h"},
				apivm.h("img", {
					class: "num-btn num-decrease",
					src: "../../image/icon/icon-cart-num-decrease.png",
					onClick: function() {
						return this$1.countChange(-1);
					}
				}),
				apivm.h("text", {class: "num-text"}, this.props.count),
				apivm.h("img", {
					class: "num-btn num-increase",
					src: "../../image/icon/icon-cart-num-increase.png",
					onClick: function() {
						return this$1.countChange(1);
					}
				})
			);
		};

		return GoodsCounter;
	})(Component);
	apivm.define("goods-counter", GoodsCounter);

	var GoodsAdd = /*@__PURE__*/ (function(Component) {
		function GoodsAdd(props) {
			Component.call(this, props);
			this.data = {
				background: "rgba(0,0,0,0)",
				goods: false,
				goodsCardBottom: 375,
				count: 1
			};
		}

		if (Component) GoodsAdd.__proto__ = Component;
		GoodsAdd.prototype = Object.create(Component && Component.prototype);
		GoodsAdd.prototype.constructor = GoodsAdd;
		GoodsAdd.prototype.installed = function() {
			var this$1 = this;
			this.data.goods = this.props.goods ? this.props.goods : api.pageParam.goods;
			console.log(this.data.goods);
			setTimeout(function() {
				this$1.data.background = "rgba(0,0,0,.5)";
				this$1.data.goodsCardBottom = 0;
			}, 11);
			var cartList = api.getPrefs({sync: true, key: "CART-DATA"});
			if (cartList) {
				cartList = JSON.parse(cartList);
				this.data.cartData = cartList[this.data.goods.id];
				if (this.data.cartData) {
					this.data.count = this.data.cartData.count;
				}
			}
		};
		GoodsAdd.prototype.closeThis = function() {
			this.data.background = "rgba(0,0,0,0)";
			this.data.goodsCardBottom = 375;
			setTimeout(function() {
				if (isMP()) {
					api.sendEvent({
						name: "CLOSE-GOODS-ADD"
					});
				} else {
					api.closeFrame();
				}
			}, 300);
		};
		GoodsAdd.prototype.countChange = function(e) {
			this.data.count += e.detail.change;
		};
		GoodsAdd.prototype.addCart = function() {
			var cartList = api.getPrefs({sync: true, key: "CART-DATA"}) || "{}";
			cartList = JSON.parse(cartList);
			cartList[this.data.goods.id] = {
				goods: this.data.goods,
				count: this.data.count
			};

			api.setPrefs({
				key: "CART-DATA",
				value: cartList
			});

			setTabBarBadge(2, Object.keys(cartList).length);
			api.toast({
				msg: "成功加入" + this.data.count + "个到购物车",
				location: "middle"
			});

			this.closeThis();
		};
		GoodsAdd.prototype.render = function() {
			var this$1 = this;
			return apivm.h(
				"view",
				{class: "page", style: "background:" + this.data.background},
				apivm.h("view", {class: "mask", onClick: this.closeThis}),
				apivm.h(
					"view",
					{
						class: "move",
						style: "transform:translateY(" + this.data.goodsCardBottom + "px)"
					},
					apivm.h(
						"view",
						{class: "goods-card"},
						apivm.h("img", {
							class: "goods-cover",
							mode: "aspectFill",
							src: this.data.goods.pic_main,
							alt: ""
						}),
						apivm.h(
							"view",
							{class: "goods-info"},
							apivm.h("text", {class: "goods-title"}, this.data.goods.name),
							apivm.h(
								"view",
								{class: "goods-footer"},
								apivm.h("text", {class: "goods-price-signal"}, "¥"),
								apivm.h("text", {class: "goods-price"}, this.data.goods.curt_price),
								apivm.h(GoodsCounter, {
									onCountChange: this.countChange.bind(this),
									count: this.data.count
								})
							)
						)
					),

					apivm.h(GoodsAction, {
						goods: this.data.goods,
						count: this.data.count,
						onAddCart: function(event) {
							if (this$1.addCart) {
								this$1.addCart(event);
							} else {
								addCart(event);
							}
						}
					})
				)
			);
		};

		return GoodsAdd;
	})(Component);
	apivm.define("goods-add", GoodsAdd);

	var MainMenu = /*@__PURE__*/ (function(Component) {
		function MainMenu(props) {
			Component.call(this, props);
			this.data = {
				CD: new Date().getTime(),
				categoryIndex: 0,
				menuList: {},
				scrollTo: 0,
				currentGoods: null
			};
			this.compute = {
				categoryNames: function() {
					return Object.keys(this.data.menuList);
				},
				offsetList: function() {
					var goodsHeight = 90;
					var emptyHeight = 88;
					var goodsNameHeight = 18;
					var offsetList = [0];
					var offset = 0;
					for (var category in this.data.menuList) {
						var goodsLength = this.data.menuList[category].length;
						offset +=
							(goodsLength ? goodsLength * goodsHeight : emptyHeight) +
							goodsNameHeight;
						offsetList.push(offset);
					}
					return offsetList;
				}
			};
		}

		if (Component) MainMenu.__proto__ = Component;
		MainMenu.prototype = Object.create(Component && Component.prototype);
		MainMenu.prototype.constructor = MainMenu;
		MainMenu.prototype.apiready = function() {
			var this$1 = this;
			this.getMenuData();
			api.addEventListener({name: "CLOSE-GOODS-ADD"}, function(_) {
				wx.showTabBar();
				this$1.data.currentGoods = null;
			});
		};
		MainMenu.prototype.switchCategory = function(index) {
			this.data.categoryIndex = index;
			this.data.CD = new Date().getTime() + 500; // 手动切换分类后需要锁定500毫秒 避免右侧scroll-view滚动时带来次生问题
			this.data.scrollTo = this.offsetList[index];
		};
		MainMenu.prototype.onScroll = function(e) {
			var y = isMP() ? e.detail.scrollTop : e.detail.y;

			for (var i = 0; i < this.offsetList.length; i++) {
				if (i) {
					// 第一个不判断
					if (y >= this.offsetList[i - 1] && y < this.offsetList[i]) {
						//判断是否在区间内
						if (new Date().getTime() > this.data.CD) {
							//冷却CD已经不存在
							this.data.categoryIndex = i - 1; //设置左边的高亮
						}
					}
				}
			}
		};
		MainMenu.prototype.openAdd = function(goods) {
			if (isMP()) {
				this.data.currentGoods = goods;
				wx.hideTabBar();
			} else {
				api.openFrame({
					name: "goods_add",
					url: "../goods_add/goods_add.stml",
					pageParam: {goods: goods}
				});
			}
		};
		MainMenu.prototype.getMenuData = function() {
			var this$1 = this;

			GET("goods_categories/goods_list").then(function(data) {
				var menuList = {};
				data.forEach(function(item) {
					menuList[item.name] = item.goods_list;
				});
				this$1.data.menuList = menuList;
			});
		};
		MainMenu.prototype.render = function() {
			var this$1 = this;
			return apivm.h(
				"view",
				{class: "main-menu"},
				apivm.h(
					"safe-area",
					null,
					apivm.h(
						"view",
						{class: "header"},
						apivm.h("text", {class: "title"}, "菜单")
					)
				),

				apivm.h(
					"view",
					{class: "menu-wrap flex-h flex-1"},
					apivm.h(
						"scroll-view",
						{"scroll-y": true, class: "left-category"},
						Object.entries(this.categoryNames).map(function(ref) {
							var index$1 = ref[0];
							var item$1 = ref[1];
							return apivm.h(
								"view",
								{
									class:
										this$1.data.categoryIndex === index$1
											? "category-item-current"
											: "category-item-normal",
									onClick: function() {
										return this$1.switchCategory(index$1);
									}
								},
								apivm.h(
									"text",
									{
										class:
											"category-text " +
											(this$1.data.categoryIndex === index$1
												? "category-text-current"
												: "category-text-normal")
									},
									item$1
								)
							);
						})
					),
					apivm.h(
						"scroll-view",
						{
							"scroll-y": true,
							"scroll-with-animation": true,
							class: "right-list flex-1",
							id: "right-list",
							onScroll: this.onScroll,
							"scroll-top": this.data.scrollTo
						},
						Object.entries(this.data.menuList).map(function(ref) {
							var index$1 = ref[0];
							var item$1 = ref[1];
							return apivm.h(
								"view",
								{id: this$1.categoryNames[index$1]},
								apivm.h(
									"view",
									null,
									apivm.h(
										"text",
										{class: "category-title"},
										this$1.categoryNames[index$1]
									)
								),
								item$1.length
									? apivm.h(
											"view",
											null,
											Object.entries(item$1).map(function(ref) {
												var goods = ref[1];
												return apivm.h(
													"view",
													{class: "goods-item flex-h"},
													apivm.h("img", {
														class: "goods-pic",
														mode: "aspectFill",
														src: goods.thumbnail,
														alt: ""
													}),
													apivm.h(
														"view",
														{class: "goods-info flex-1"},
														apivm.h("text", {class: "goods-name flex-1"}, goods.name),
														apivm.h(
															"view",
															{class: "flex-h"},
															apivm.h("text", {class: "goods-price-signal"}, "¥"),
															apivm.h(
																"text",
																{class: "flex-1 goods-price-num"},
																goods.curt_price.toFixed(2)
															),
															apivm.h("img", {
																class: "num-btn num-increase",
																onClick: function() {
																	return this$1.openAdd(goods);
																},
																src: "../../image/icon/icon-cart-num-increase.png"
															})
														)
													)
												);
											})
									  )
									: apivm.h(
											"view",
											{class: "goods-empty flex flex-center"},
											apivm.h("text", {class: "goods-empty-text"}, "当前分类下暂无商品"),
											apivm.h(
												"text",
												{class: "goods-empty-text"},
												"There is no goods in this classification"
											)
									  )
							);
						})
					)
				),

				this.data.currentGoods
					? apivm.h(
							"view",
							{style: "position: absolute;width: 100%;height: 100%;"},
							apivm.h(GoodsAdd, {goods: this.data.currentGoods})
					  )
					: null
			);
		};

		return MainMenu;
	})(Component);
	apivm.define("main-menu", MainMenu);
	apivm.render(apivm.h("main-menu", null), "body");
})();

	window['createApp'] = function() {
		apivm.$_start();
	}
	return avm;
}